package com.sooncode.project.focusservice.model;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener;
import com.sooncode.project.core.finder.Finder;
import com.sooncode.project.core.model.DomainException;
import com.sooncode.project.core.session.DomainSession;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Map;
@Data
public class MemberImport implements ReadListener<MemberImport> {
    @ExcelProperty("姓名")
    private String name;
    @ExcelProperty("性别")
    private String sex;
    @ExcelProperty("出生日期")
    private String birth;
    @ExcelProperty("家长姓名")
    private String parent_name;
    @ExcelProperty("联系电话")
    private String phone;
    @ExcelProperty("班级")
    private String className;
    @ExcelProperty("备注")
    private String notes;
    @ExcelIgnore
    private String orgId;
    @ExcelIgnore
    private Organization org;
    @ExcelIgnore
    private DomainSession session;

    public MemberImport(String orgId, DomainSession session){
        setOrgId(orgId);
        org=new Finder<>(Organization.class).byId(this.getOrgId());
        setSession(session);
    }
    public MemberImport(){}
    @Override
    public void invoke(MemberImport importer, AnalysisContext analysisContext) {
        if(StringUtils.isEmpty(importer.getName())
            || StringUtils.isEmpty(importer.getSex())
            || StringUtils.isEmpty(importer.getBirth())){
            throw new DomainException(String.format("数据不完整,错误行:%s",analysisContext.readRowHolder().getRowIndex()));
        }
        Member member=new Member();
        session.add(member);
        member.setOrgId(this.getOrgId());
        member.setName(importer.getName());
        member.setSex(importer.getSex());
        member.setBirth(importer.getBirth());
        member.setParent_name(importer.getParent_name());
        member.setNotes(importer.getNotes());
        member.setPhone(importer.getPhone());
        if(StringUtils.isNotEmpty(importer.getClassName())){
            Classes classes=new Finder<>(Classes.class)
                .byField("name",importer.getClassName())
                .and("orgId",this.getOrg())
                .and("status","normal")
                .first();
            if(classes!=null) member.setClassId(classes.getId());
        }
        member.setCreateDate(new Date());
        member.add(org);
        member.updateAge();
    }
    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        ReadListener.super.onException(exception, context);
    }

    @Override
    public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
        ReadListener.super.invokeHead(headMap, context);
    }
    @Override
    public void extra(CellExtra extra, AnalysisContext context) {
        ReadListener.super.extra(extra, context);
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
